package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.csiec.clock.activity.MainActivity;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.bean.login.ResultCode;
import com.csiec.clock.callback.LoginCallBack;
import com.csiec.clock.databinding.LoginFragmentBinding;
import com.csiec.clock.model.LoginModel;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class LoginViewModel extends ViewModel {

    private LoginModel model;

    private MutableLiveData<String> userName;
    private MutableLiveData<String> passWord;
    private MutableLiveData<Boolean> isChecked;
    private Application application;
    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private LoginFragmentBinding binding;
    private final Handler handler = new Handler();

    public void init(Activity activity, LoginFragmentBinding binding) {
        this.application = activity.getApplication();
        this.activity = activity;
        this.binding = binding;
        model = new LoginModel();
        checkUser();
        if (userName == null) userName = new MutableLiveData<>();
        if (passWord == null) passWord = new MutableLiveData<>();
        if (isChecked == null) isChecked = new MutableLiveData<>();
        isChecked.setValue(true);
    }

    //确定是否有sharePres
    private void checkUser() {
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        boolean isLogin = preferences.getBoolean("isLogin", false);
        final String number = preferences.getString("username", "");
        final String password = preferences.getString("password", "");
        if (isLogin) {
            model.login(number, password, new LoginCallBack() {
                @Override
                public void onSuccess(ResultCode resultCode) {
                    if (resultCode.getStatus() == 200) {
                        //存储基本信息
                        UserInfo.setUserInfo(resultCode.getData().getData());
                        saveSomeMessage(number, password);
                        Intent intent = new Intent(application, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        application.startActivity(intent);
                        activity.finish();
                    } else {
                        handler.post(() -> showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "用户名或密码错误"));
                    }
                }

                @Override
                public void onFailed() {
                    handler.post(() -> showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络错误或其它错误"));
                }
            });
        }

    }

    public void login() {
        //登录
        if (userName.getValue() != null && passWord.getValue() != null && isChecked.getValue() != null) {

            model.login(userName.getValue(), passWord.getValue(), new LoginCallBack() {
                @Override
                public void onSuccess(ResultCode resultCode) {
                    if (resultCode.getStatus() == 200) {
                        //存储基本信息
                        UserInfo.setUserInfo(resultCode.getData().getData());
                        saveSomeMessage(userName.getValue(), passWord.getValue());
                        Intent intent = new Intent(application, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        application.startActivity(intent);
                        activity.finish();
                    } else {
                        handler.post(() -> showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "用户名或密码错误"));
                    }
                }

                @Override
                public void onFailed() {
                    handler.post(() -> showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络错误或其它错误"));
                }
            });
        } else {
            showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "用户名或密码不能为空");
        }

    }

    private void saveSomeMessage(String number, String password) {
        SharedPreferences.Editor editor = application.getSharedPreferences("data", Context.MODE_PRIVATE).edit();
        editor.putString("username", number);
        editor.putString("password", password);
        editor.putBoolean("isLogin", true);
        editor.apply();
    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.btnRegister.postDelayed(tipDialog::dismiss, 2000);
    }

    public MutableLiveData<String> getUserName() {
        return userName;
    }

    public MutableLiveData<String> getPassWord() {
        return passWord;
    }

    public MutableLiveData<Boolean> getIsChecked() {
        return isChecked;
    }
}
